﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FlexiCommerce.Installation
{
    public class DatabaseStatus
    {
        private bool isConnected = false;
        private bool schema = false;
        private bool installation = false;
        private int items = 0;
        private int details = 0;
        private int detailCollections = 0;
        private int authorizedRoles = 0;
        private string connectionError = null;
        private Exception connectionException = null;
        private string schemaError = null;
        private Exception schemaException = null;
        private int rootItemID = 0;
        private int startPageID = 0;
        private string itemsError = null;
        private string connectionType = null;

        public string ConnectionType
        {
            get { return connectionType; }
            set { connectionType = value; }
        }

        public int AuthorizedRoles
        {
            get { return authorizedRoles; }
            set { authorizedRoles = value; }
        }

        public int DetailCollections
        {
            get { return detailCollections; }
            set { detailCollections = value; }
        }

        public int Details
        {
            get { return details; }
            set { details = value; }
        }

        public int Items
        {
            get { return items; }
            set { items = value; }
        }

        public bool HasSchema
        {
            get { return schema; }
            set { schema = value; }
        }

        public bool IsConnected
        {
            get { return isConnected; }
            set { isConnected = value; }
        }

        public string ConnectionError
        {
            get { return connectionError; }
            set { connectionError = value; }
        }

        public string SchemaError
        {
            get { return schemaError; }
            set { schemaError = value; }
        }

        public Exception ConnectionException
        {
            get { return connectionException; }
            set { connectionException = value; }
        }

        public Exception SchemaException
        {
            get { return schemaException; }
            set { schemaException = value; }
        }


        public bool IsInstalled
        {
            get { return installation; }
            set { installation = value; }
        }

        public string ItemsError
        {
            get { return itemsError; }
            set { itemsError = value; }
        }

        public string ToStatusString()
        {
            if (IsConnected)
            {
                return string.Format(
                    "The connection to the database seems fine but the database tables might not be created (error message: {0})",
                    SchemaError);
            }
            else
            {
                return string.Format(
                    "No database or not properly configured connection string (error message: {0}",
                    ConnectionError);
            }
        }


        public string ToConnectionStatusString()
        {
            return IsConnected
                    ? "Database connection OK."
                    : string.Format("<span title='{0}'>Couldn't open connection to database.</span><!--{1}-->", ConnectionError, ConnectionException);
        }
    }
}
